Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

nice_things/fs/read_link.sh

Since 0.3.0 · Source

import "{ read_link }" from nice_things/fs/read_link.sh

Synopsis
read_link <out_var> <file>

Configuration

Description
Get value of a symbolic link.

Note

This is not a pure sh implementation because it depends on the external ls utility to read the target of the symlink. This works consistently because the output of ls is strictly specified in POSIX, but it means this function can be slower than most other functions in the framework.

Options

Operands

  • <out_var>: Output variable; the result will be written to this variable.
  • <file>: Path to a symbolic link.

Stdin

Stdout

Stderr

Exit status

  • 0: Successful completion.
  • 22: <file> is not a symbolic link.
  • 123: Unexpected error.

Abort

Usage examples

# Read the value of a link
link=/path/to/link
read_link link_target "$link" || {
	case $? in
	22) log_error "File is not a symbolic link: '${link}'" ;;
	*) log_error "Unexpected error trying to read link at '${link}'" ;;
	esac
}